﻿using System;
using Avalonia.Controls;
using Avalonia.Interactivity;
using FluentAvalonia.UI.Controls;

namespace LuckyFish.CalculatorFluent.Pages;

public partial class CalendarView : UserControl
{
    public CalendarView()
    {
        InitializeComponent();
        Start.SelectedDate = End.SelectedDate = DateTime.Today;
        ComboBox.ItemsSource = new[]
        {
            "相隔时间",
            "天数相差"
        };
        ComboBox.SelectedItem = "相隔时间";
    }

    private void DateChanged(object? sender, SelectionChangedEventArgs e)
    {
        var day = (End.SelectedDate - Start.SelectedDate)?.TotalDays ?? 0;
        DifferenceBlock.Text = day % 7 == 0 && day != 0 ? $"{day / 7}周" :
            Math.Abs(day) > 0 ? $"相差{day}天" : "相同天数";
    }

    private void ComboChanged(object? sender, SelectionChangedEventArgs e)
    {
        var s = e.AddedItems[0] as string;
        AddPanel.IsVisible = s != "相隔时间";
        DifferenceBlock.Text = s == "相隔时间" ? "相同天数" : "";
        DiffTextBlock.Text = s == "相隔时间" ? "差值" : "日期";
    }

    private void AddCheckedChanged(object? sender, RoutedEventArgs e)
    {
        if(Start == null! || Start.SelectedDate == null)return;
        var date = Start.SelectedDate ?? DateTime.Today;
        date = date.AddDays(RadioButton.IsChecked ?? true ? DayValue.Value : -DayValue.Value);
        date = date.AddMonths(RadioButton.IsChecked ?? true ? (int)MouthValue.Value : -(int)MouthValue.Value);
        date = date.AddYears(RadioButton.IsChecked ?? true ? (int)YearValue.Value : -(int)YearValue.Value);
        DifferenceBlock.Text = date.ToString("yyyy年M月d日");
    }

    private void NumberChanged(NumberBox sender, NumberBoxValueChangedEventArgs args)
    {
        if(Start == null! || Start.SelectedDate == null)return;
        var date = Start.SelectedDate ?? DateTime.Today;
        date = date.AddDays(RadioButton.IsChecked ?? true ? DayValue.Value : -DayValue.Value);
        date = date.AddMonths(RadioButton.IsChecked ?? true ? (int)MouthValue.Value : -(int)MouthValue.Value);
        date = date.AddYears(RadioButton.IsChecked ?? true ? (int)YearValue.Value : -(int)YearValue.Value);
        DifferenceBlock.Text = date.ToString("yyyy年M月d日");
    }
}